package com.qingfeng.client.bean;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.qingfeng.base.model.TMenuResource;
import com.qingfeng.base.model.TRole;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/2/25 10:15 上午
 */
@ApiModel(value = "com.qingfeng.client.bean.Seller")
@Data
@TableName(value = "seller")
public class Seller implements UserDetails {
    /**
     * 卖家id
     */
    @TableId(value = "id", type = IdType.AUTO)
    @ApiModelProperty(value = "卖家id")
    private Long id;

    /**
     * 卖家属于那个店铺
     */
    @TableField(value = "store_id")
    @ApiModelProperty(value = "卖家属于那个店铺")
    private Integer storeId;

    /**
     * 用户名
     */
    @TableField(value = "name")
    @ApiModelProperty(value = "用户名",required = true)
    private String name;

    /**
     * 手机号
     */
    @TableField(value = "phone")
    @ApiModelProperty(value = "手机号",required = true)
    private String phone;

    /**
     * 密码
     */
    @TableField(value = "password")
    @ApiModelProperty(value = "密码",required = true)
    private String password;

    /**
     * 性别,1：男，2：女
     */
    @TableField(value = "sex")
    @ApiModelProperty(value = "性别,1：男，2：女",required = true)
    private Integer sex;

    /**
     * 年龄
     */
    @TableField(value = "age")
    @ApiModelProperty(value = "年龄",required = true)
    private Integer age;

    /**
     * qq号
     */
    @TableField(value = "qq")
    @ApiModelProperty(value = "qq号",required = false)
    private String qq;

    /**
     * 用户登录令牌
     */
    @TableField(value = "token")
    @ApiModelProperty(value = "用户登录令牌",required = false)
    private String token;

    /**
     * 用户真实姓名
     */
    @TableField(value = "real_name")
    @ApiModelProperty(value = "用户真实姓名",required = true)
    private String realName;

    /**
     * 证件类型：1：身份证2：护照3：港台回乡证4：港澳居民内地通行证
     */
    @TableField(value = "auth_type")
    @ApiModelProperty(value = "证件类型：1：身份证2：护照3：港台回乡证4：港澳居民内地通行证",required = true)
    private Integer authType;

    /**
     * 证件账号
     */
    @TableField(value = "auth_account")
    @ApiModelProperty(value = "证件账号",required = true)
    private String authAccount;

    /**
     * 国家手机号编码
     */
    @TableField(value = "country_phone_code")
    @ApiModelProperty(value = "国家手机号编码,如中国82",required = true)
    private String countryPhoneCode;

    /**
     * 排序
     */
    @TableField(value = "sort")
    @ApiModelProperty(value = "排序")
    private Integer sort;

    /**
     * 失败次数
     */
    @TableField(value = "wrong_count")
    @ApiModelProperty(value = "失败次数")
    private Integer wrongCount;

    /**
     * 账号是否冻结状态，1标识未冻结
     */
    @TableField(value = "enable")
    @ApiModelProperty(value = "账号是否冻结状态，1标识未冻结")
    private Integer enable;

    /**
     * 最近一次的登录时间
     */
    @TableField(value = "last_login_time")
    @ApiModelProperty(value = "最近一次的登录时间")
    private Date lastLoginTime;

    /**
     * 上次修改密码的时间，后续进行密码过期提醒
     */
    @TableField(value = "last_upd_passwd_time")
    @ApiModelProperty(value = "上次修改密码的时间，后续进行密码过期提醒")
    private Date lastUpdPasswdTime;


    @TableField(exist = false)
    private List<TRole> roles;

    @TableField(exist = false)
    private List<TMenuResource> resources;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        if (roles == null){
            return authorities;
        }
        for (TRole role : roles){
            authorities.add(new SimpleGrantedAuthority(role.getRoleName()));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return name;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        if (enable == 1){
            return true;
        }
        return false;
    }

}
